/*
 * Copyright 2009-2010 Belmont Software Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.belmont.backup.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

/**
 * @author Jaffer
 *
 */
public  class CheckBoxNodeRenderer implements TreeCellRenderer {
    private BNRCheckBox leafRenderer = new BNRCheckBox();
    private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();
    Color selectionBorderColor, selectionForeground, selectionBackground, textForeground, textBackground;

    protected BNRCheckBox getLeafRenderer() {
	return leafRenderer;
    }

    public CheckBoxNodeRenderer() {
	Font fontValue;
	fontValue = UIManager.getFont("Tree.font");
	if (fontValue != null) {
	    leafRenderer.setFont(fontValue);
	}
	Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
	leafRenderer.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));

	selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
	selectionForeground = UIManager.getColor("Tree.selectionForeground");
	selectionBackground = UIManager.getColor("Tree.selectionBackground");
	textForeground = UIManager.getColor("Tree.textForeground");
	textBackground = UIManager.getColor("Tree.textBackground");
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
						  boolean leaf, int row, boolean hasFocus) {

	Component returnValue;

	if (leaf) {
	    String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
	    leafRenderer.setText(stringValue);
	    leafRenderer.setSelected(false);

	    leafRenderer.setEnabled(tree.isEnabled());

	    if (selected) {
		leafRenderer.setForeground(selectionForeground);
		leafRenderer.setBackground(selectionBackground);
	    } else {
		leafRenderer.setForeground(textForeground);
		leafRenderer.setBackground(textBackground);
	    }

	    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
		Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
		if (userObject instanceof CheckBoxNode) {
		    CheckBoxNode node = (CheckBoxNode) userObject;
		    leafRenderer.setText(node.getName()+" "+node.getToolTip());
		    leafRenderer.setFullPath(node.getFullPath());
		    leafRenderer.setSelected(node.isSelected());
		    leafRenderer.name = node.getName();
		    leafRenderer.modifiedDate = node.getModifiedDate();
		    leafRenderer.checksum = node.getChecksum();
		    leafRenderer.size = node.getSize();
		}
	    }
	    returnValue = leafRenderer;
	} else {
	    returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree, value, selected,
								       expanded, leaf, row,
								       hasFocus);
	}
	return returnValue;
    }
}